<?php

// No direct access to this file
defined('_JEXEC') or die;
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');
class JFormFieldCategories extends JFormFieldList{
	protected $type = 'categories';
	protected function getOptions(){
		$db = JFactory::getDBO();
		//$query = new JDatabaseQuery;
		$query = $db->getQuery(true);
		$query->select('#__categories.title as category,id');
		$query->from('#__categories');
		$query->where('extension="com_raovat"');
		
		$db->setQuery((string)$query);
		$messages = $db->loadObjectList();
		$options = array();
		$options[] = JHtml::_('select.option','','(Chọn loại Quảng cáo Rao vặt)');
		if ($messages)
		{
			foreach($messages as $message) 
			{
				if (!$this->checkParent($message->id)){
					$options[] = JHtml::_('select.option',$message->id, $message->category);
				}
			}
		}
		$options = array_merge(parent::getOptions(), $options);
		return $options;
	}
	function checkParent($id)
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select('count(*)');
		$query->from('#__categories');
		$query->where('parent_id='.$id);
		$db->setQuery((string)$query);
		return $db->loadResult();
	}
}
